$(document).ready(function(){
    var current_step = 0;
    var current_index = 0;
    var total_count = 0;
    var order_texts = new Array();
    
    init_text_object(total_count);
    $("#order-steps a").click(function(){
        $(this).addClass("current").parent("li").siblings("li").children("a").removeClass("current");
        current_step = parseInt($(this).parent("li").attr("rel"));
        stepID = "#step-" + current_step;
        $(stepID).addClass("show").siblings("div.order-detail").removeClass("show");
    })
    $("div.order-detail a.next-step").click(function(){
        $("#order-steps li").eq(current_step+1).children("a").click();
    })
    $("div.order-detail a.prev-step").click(function(){
        $("#order-steps li").eq(current_step-1).children("a").click();
    })
    //Step 2
    $(".choose-font").live('click',function(){
        current_index = $("div.order-text-box").index($(this).parent("div.order-text-box"));
        order_texts[current_index].text = $(this).siblings("input.order-text").val();
        $("#font-selection").show();
    })
    $("#font-selection input").click(function(){
        $("div.order-text-box").eq(current_index).children(".choose-font").addClass('font-chosen').html($(this).val());
        $("div.order-text-box").each(function(){
            if(!$(this).hasClass('hidden')) order_texts[$("div.order-text-box").index($(this))].text = $(this).children("input.order-text").val();
        });
        order_texts[current_index].font = $(this).val();
        $("#font-selection").hide();
        if(order_texts[current_index].text != "") ajax_preview();
    })
    $("input.order-text").live('blur',function(){
        order_text_index = $("div.order-text-box").index($(this).parent("div.order-text-box"));
        if(order_texts[order_text_index].text != $(this).val()) {
            order_texts[order_text_index].text = $(this).val();
            if(order_texts[order_text_index].font != "") ajax_preview();
        }

    })
    $("#add-more-text").click(function(){
        var new_clone = $("div.order-text-box.hidden").clone().removeClass("hidden");
        $("div.order-text-box.hidden").before(new_clone);
        init_text_object($("div.order-text-box").index(new_clone));
    })
    $(".remove-this-text").live('click',function(){
        current_index = $("div.order-text-box").index($(this).parent("div.order-text-box"));
        $(this).parent("div.order-text-box").remove();
        removed_order_text = order_texts[current_index];
        order_texts.splice(current_index,1);
        if(removed_order_text.text != "" && removed_order_text.font != "") ajax_preview();
    })

    function init_text_object(new_index)
    {
        order_texts[new_index] = new Object();
        order_texts[new_index].text = '';
        order_texts[new_index].font = '';
    }
    
    function show_popup(msg)
    {
        alert(msg);
    }
    
    function ajax_preview()
    {
        $.ajax({
            type:       'POST',
            url:        var_site_url+'order/ajax_preview',
            dataType:   'json',
            data:       {order_texts:order_texts},
            success:    function(return_data)
                        {
                            if(!return_data["success"])
                            {
                                show_popup(msg_error);
                            }
                            else
                            {
                                $("#order-text-preview").html('<img src="' + return_data["data"]["image_url"] + '" />');
                            }
                        },
            error:      function()
                        {
                            show_popup(msg_error);
                        }
        })
    }
})